using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Maze;
using Nine;
using Nine.Components;
using Nine.Graphics;
using Nine.Graphics.Primitives;
using Nine.Graphics.ObjectModel;
using Nine.Navigation;
using Ruminate;
using Ruminate.GUI;
using Ruminate.GUI.Content;
using Ruminate.GUI.Framework;
using Ruminate.Utils;
using TestBed;
using System.Threading;
using TestBed.Ristin_nameSpace;
namespace EaWebMaze
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        RuminateGUI gui;


        //---def----
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        ModelBatch modelBatch;
        BasicEffect kockaEfekt;
        BasicEffect kockaEfektPath;
        //----------
        //---ekran----
        private int BackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width - 100;
        private int BackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height - 100;
        //-----------
        //-----menu----
        Texture2D rect;
        SpriteFont labele;
        //-----------
        //-----teren-----
        DrawableSurface terrain;
        RasterizerState wireframe;
        BasicEffect terrainEffect;
        //------------
        //----kamera----
        TopDownEditorCamera camera;
        Vector3 cameraPosition = new Vector3(64.5f, -33.4f , 102f);
        float leftrightRot = 0.059f;
        float updownRot = 0.77f;
        float rotationSpeed = 0.05f;
        float moveSpeed = 40.0f;
        MouseState originalMouseState;
        Matrix viewMatrix;
        Matrix projectionMatrix;
        GraphicsDevice device;
        //-------------
        //sve vezano za lavirint
        private static MazeCreator _creator;
        public static byte[,] maze;
        int mazeSize = 13;
        float wallHeight = 5f;
        Cube[,] mazeCube;
        //------PARAMETRI-----

      
        String[] TipMutacije = { "Random", "Exchange", "Displacement", "Scramble", "Insertion", "Inversion", "DisplacedInversion" };

        String[] TipSelekcije = { "RouletteWheel", "Tournament", "AlternateTournament" };

        String[] TipUkrstanja = { "OnePoint", "TwoPoints", "HalfUniform" };

        String[] TipSkaliranja = { "Rank", "Sigma", "Boltzmann" };

        int INDEKSM = 1;
        int INDEKSS =1;
        int INDEKSU = 1;
        int INDEKSSK = 1;
        int statusSolution = 0;
        int PopulationSize = 50;
       public  int ChromozomeLength = 50;
        float CrossScale = 0.7f;
        float MutaionScale = 0.02f;
        //----------------
      public   bool animStart = false;
      string resenje = "Nije nadjeno";
      ArrayOfInt kretanje;
      Thread serviceCall;

        //--------------
        public Service1SoapClient c = new Service1SoapClient();
        Thread bestPath;
        //------------
        
        Random rnd2;// = new Random();
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
           
          
            graphics.SynchronizeWithVerticalRetrace = false;
            graphics.PreferredBackBufferWidth = BackBufferWidth;
            graphics.PreferredBackBufferHeight = BackBufferHeight;
            IsMouseVisible = true;
            IsFixedTimeStep = false;
            Window.AllowUserResizing = false;
            graphics.PreferMultiSampling = true;
            graphics.ApplyChanges();
         
            gui = new RuminateGUI(this);
            gui.SetTheme(new  NewTheme(Content, gui));

            Window.Title = "EaWebMazeApplication";

            base.Initialize();
        }

      
        protected override void LoadContent()
        {
           
            Components.Add(new InputComponent(Window.Handle));
            spriteBatch = new SpriteBatch(GraphicsDevice);
            camera = new TopDownEditorCamera(GraphicsDevice);
            modelBatch = new ModelBatch(GraphicsDevice);
            kockaEfekt = new BasicEffect(GraphicsDevice);
            kockaEfektPath = new BasicEffect(GraphicsDevice);
            kockaEfekt.TextureEnabled = true;
            kockaEfekt.Texture = Content.Load<Texture2D>("box");

            kretanje = new ArrayOfInt();

           serviceCall = new Thread(callEA);
                   

            kockaEfektPath.TextureEnabled = true;

            rect = Content.Load<Texture2D>("rect");
            labele = Content.Load<SpriteFont>("SpriteFont1");

            kockaEfektPath.Texture = Content.Load<Texture2D>("Brick250a");

            device = GraphicsDevice;
            terrain = new DrawableSurface(GraphicsDevice, Content.Load<Heightmap>("heightmap"), 32);
            terrainEffect = new BasicEffect(GraphicsDevice);
            terrain.LightingEnabled = true;
            terrainEffect.TextureEnabled = true;
            terrainEffect.Texture = Content.Load<Texture2D>("Mountain");
            terrainEffect.PreferPerPixelLighting = true;
            terrainEffect.AmbientLightColor = Vector3.One * 0.1f;
            terrainEffect.DirectionalLight0.DiffuseColor = Vector3.One * 0.1f;
            terrainEffect.DirectionalLight0.SpecularColor = Vector3.Zero;
            terrainEffect.DirectionalLight0.Direction = new Vector3(0, -0.707107f, -0.707107f);


            kockaEfekt.EnableDefaultLighting();
            kockaEfektPath.EnableDefaultLighting();
          //  terrainEffect.EnableDefaultLighting();
           
            //----lavirint---------
            _creator = new MazeCreator(mazeSize, mazeSize, new System.Drawing.Point(1, 1));
            maze = _creator.CreateMaze();
            
            
        /********************** Ispisivanje lavirinta u konzolu *********************/

           for(int i=0; i<mazeSize;i++)
           {
               Console.WriteLine();

               for(int j=0; j< mazeSize; j++)
               {
                   Console.Write(maze[i, j]);
               }
           }
 
        /*************************************************************************/      

            mazeCube = new Cube[mazeSize, mazeSize];
            for (int i = 0; i < _creator.Height; i++)
                for (int j = 0; j < _creator.Width; j++)
                    mazeCube[i, j] = new Cube(GraphicsDevice, wallHeight);
            //---------------

            rnd2 = new Random();


            var pannels = new List<Panel>();
            gui.AddElement(new Label(BackBufferWidth- BackBufferWidth/5+10, 5, "LAVIRINT"));
            //---Velicina lavirinta: - 5 +
            gui.AddElement(new Label(BackBufferWidth- BackBufferWidth/5 +10, 30, "Velicina lavirinta:"));
            gui.AddElement(new Button(BackBufferWidth- 70, 30, " -", delegate{
                if (mazeSize > 3)
                    mazeSize -= 2;
                createMaze(mazeSize);
            }));
            //Labela se iscrtava u draw

            gui.AddElement(new Button(BackBufferWidth- 20, 30, "+", delegate{
                if (mazeSize < 55)
                {
                    mazeSize += 2;

                    initialiseMaze();

                    createMaze(mazeSize);
                }
            }));
            //---Visina zidova: - 0.5 +
            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 60, "Visina zidova:"));
            gui.AddElement(new Button(BackBufferWidth - 70, 60, " -", delegate
            {
                if (wallHeight > 1f)
                    wallHeight -= 0.5f;
                initialiseMaze();
            }));
            //labela se crta u draw
            //gui.AddElement(new Label(BackBufferWidth - 45, 58, "0.5"));
            gui.AddElement(new Button(BackBufferWidth - 20, 60, "+", delegate
            {
                if (wallHeight < 5.0f)
                    wallHeight += 0.5f;
                initialiseMaze();
            }));
            
            //Antialiasing on-off predstavlja problem za program, tako da ga necemo raditi
            //gui.AddElement(new CheckBox(BackBufferWidth - 120, 100, "Antialiasing")
            //    {
            //        OnToggle = delegate
            //        {
            //            graphics.PreferMultiSampling = true;
            //            graphics.ApplyChanges();
            //        },
            //        OffToggle = delegate
            //        {
            //            graphics.PreferMultiSampling = false;
            //            graphics.ApplyChanges();
            //        }

            //    }
            //    );

           //----------KAMERA-----------
            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 130, "KAMERA"));
            //---Brzina kretanja kamere: 
            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 155, "Kretanje kamere:"));
            gui.AddElement(new Button(BackBufferWidth - 70, 157, " -", delegate
            {
                if (moveSpeed > 10f)
                    moveSpeed -= 5f;
            }));
            //labela se iscrtava u draw
            //gui.AddElement(new Label(BackBufferWidth - 44, 155, "0.1"));
            gui.AddElement(new Button(BackBufferWidth - 20, 157, "+", delegate
            {
                if (moveSpeed < 100f)
                    moveSpeed += 5f;
            }));
            //---Brzina rotiranja kamere: 
            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 177, "Rotacija kamere:"));
            gui.AddElement(new Button(BackBufferWidth - 70, 177, " -", delegate
            {
                if (rotationSpeed > 0.05f)
                    rotationSpeed -= 0.01f;
            }));
            //labela se crta u draw
            //gui.AddElement(new Label(BackBufferWidth - 44, 175, "10"));
            gui.AddElement(new Button(BackBufferWidth - 20, 177, "+", delegate
            {
                if (rotationSpeed < 0.2f)
                    rotationSpeed += 0.01f;
            }));
            //----------ALGORITAM-----------
            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 210, "ALGORITAM"));
            //---Velicina populacije: 
            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 250, "Velicina populacije:"));
            gui.AddElement(new Button(BackBufferWidth - 75, 250, " -", delegate
            {
                if (PopulationSize == 0)
                { }
                else
                    PopulationSize-=5;
            }));
            //gui.AddElement(new Label(BackBufferWidth - 50, 250, "150"));
            gui.AddElement(new Button(BackBufferWidth - 20, 250, "+", delegate
            {
                PopulationSize+=5;
            }));

            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 280, "Duzina hromozoma:"));
            gui.AddElement(new Button(BackBufferWidth - 75, 280, " -", delegate
            {
                if (ChromozomeLength == 0)
                { }
                else
                    ChromozomeLength--;
            }));
            //gui.AddElement(new Label(BackBufferWidth - 50, 280, "400"));
            gui.AddElement(new Button(BackBufferWidth - 20, 280, "+", delegate
            {
                ChromozomeLength++;
            }));

            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 310, "Stopa ukrstanja:"));
            gui.AddElement(new Button(BackBufferWidth - 75, 310, " -", delegate
            {
                if (CrossScale <= 0)
                { CrossScale = 0; }
                else
                    CrossScale-=0.1f;
            }));
            //gui.AddElement(new Label(BackBufferWidth - 50, 310, "0.7"));
            gui.AddElement(new Button(BackBufferWidth - 20, 310, "+", delegate
            {
                CrossScale += 0.1f;
            }));

            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 340, "Stopa mutacije:"));
            gui.AddElement(new Button(BackBufferWidth - 75, 340, " -", delegate
            {
                if (MutaionScale <= 0)
                { MutaionScale = 0; }
                else
                    MutaionScale -= 0.01f;
            }));
            //gui.AddElement(new Label(BackBufferWidth - 55, 340, "0.001"));
            gui.AddElement(new Button(BackBufferWidth - 20, 340, "+", delegate
            {
                MutaionScale+=0.01f;
            }));

            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 370, "Tip mutacije:"));
            gui.AddElement(new Button(BackBufferWidth - 160, 370, " <", delegate
            {
                if (INDEKSM <= 0)
                    INDEKSM = 6;
                else
                INDEKSM--;
            }));
            gui.AddElement(new Label(BackBufferWidth - 55, 370, " "));
            gui.AddElement(new Button(BackBufferWidth - 20, 370, ">", delegate
            {
                INDEKSM++;
            }));

            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 400, "Tip ukrstanja:"));
            gui.AddElement(new Button(BackBufferWidth - 160, 400, " <", delegate
            {
                if (INDEKSU <= 0)
                    INDEKSU = 2;
                else
                    INDEKSU--;
            }));
            gui.AddElement(new Label(BackBufferWidth - 55, 400, " "));
            gui.AddElement(new Button(BackBufferWidth - 20, 400, ">", delegate
            {
                INDEKSU++;
            }));

            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 430, "Tip skaliranja:"));
            gui.AddElement(new Button(BackBufferWidth - 160, 430, " <", delegate
            {
                if (INDEKSSK <= 0)
                    INDEKSSK = 2;
                else
                    INDEKSSK--;
            }));
            gui.AddElement(new Label(BackBufferWidth - 55, 430, " "));
            gui.AddElement(new Button(BackBufferWidth - 20, 430, ">", delegate
            {
                INDEKSSK++;
            }));

            gui.AddElement(new Label(BackBufferWidth - BackBufferWidth / 5 + 10, 460, "Tip selekcije:"));
            gui.AddElement(new Button(BackBufferWidth - 160, 460, " <", delegate
            {
                if (INDEKSS <= 0)
                    INDEKSS = 2;
                else
                    INDEKSS--;
            }));
            gui.AddElement(new Label(BackBufferWidth - 55, 460, " "));
            gui.AddElement(new Button(BackBufferWidth - 20, 460, ">", delegate
            {
                INDEKSS++;
            }));
            //---------------

           
            gui.AddElement(new Button(BackBufferWidth - 180, 550, " P O K R E N I ", delegate
            {
               
                    serviceCall.Start();
                   //  bestPath = new Thread(getBestPath);
                animStart = true;
            
            }));

            wireframe = new RasterizerState() { FillMode = FillMode.WireFrame, CullMode = CullMode.None };


            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.3f, 1000.0f);

            Mouse.SetPosition(device.Viewport.Width / 2, device.Viewport.Height / 2);
            originalMouseState = Mouse.GetState();

         

            UpdateViewMatrix();
            
          
        }

        private void initialiseMaze()
        {
            _creator = new MazeCreator(mazeSize, mazeSize, new System.Drawing.Point(1, 1));
            maze = _creator.CreateMaze();

            mazeCube = new Cube[mazeSize, mazeSize];
            for (int i = 0; i < _creator.Height; i++)
                for (int j = 0; j < _creator.Width; j++)
                    mazeCube[i, j] = new Cube(GraphicsDevice, wallHeight);
        }

       
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

         protected override void Update(GameTime gameTime)
        {
             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
            ProcessInput(timeDifference);
            /*if (gameTime.TotalGameTime.Milliseconds % 250 == 0)
            {
                for (int i = 0; i < 10; i++)
                {
                    kretanje[i] = 2 + rnd2.Next(3);
                }
            }*/
            
             //kretanje = Services.callEA(maze, maze.GetLength, )
            /* Service1SoapClient c = new Service1SoapClient();
             ArrayOfInt[] maze1=  new ArrayOfInt[mazeSize];
             ArrayOfInt niz= new ArrayOfInt();
             for (int i = 0; i < mazeSize; i++)
             {
                 niz.Clear();
                 for (int j = 0; j < mazeSize; j++)
                     niz.Add(maze[i, j]);
                 maze1[i] = niz;
             }
             ArrayOfInt rez= c.callEA( maze1,
                 mazeSize,
                     mazeSize - 1,
                     mazeSize - 1,
                     0,
                     0,
                     2 * mazeSize,
                     30,
                     "Random",
                     "Tournament",
                     "OnePoint",
                     "Rank",
                     0.02,
                     0.5,
                     5);

             for (int i = 0; i < rez.Count; i++)
             {

                 Console.Write(rez.ElementAt(i));
             }
             Console.WriteLine();
             */
         //   if (animStart == true)
           // {
                if (gameTime.TotalGameTime.Milliseconds % 100 == 0)
                      //bestPath.Start();
                      //bestPath.Abort();
             if(statusSolution == 0)
                    getBestPath();
            //}
                 base.Update(gameTime);
        }

         protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

             //teren--------

            Vector3 eyePosition = Matrix.Invert(viewMatrix).Translation;
            terrain.UpdateLevelOfDetail(eyePosition);
            // Draw the terrain
            BoundingFrustum frustum = new BoundingFrustum(viewMatrix * projectionMatrix);


            modelBatch.Begin(ModelSortMode.Deferred, viewMatrix, projectionMatrix, BlendState.AlphaBlend, SamplerState.LinearWrap, null,
                Keyboard.GetState().IsKeyDown(Keys.Space) ? wireframe : null);
            {
                foreach (var patch in terrain.Patches)
                {
                    // Cull patches that are outside the view frustum
                    if (frustum.Contains(patch.BoundingBox) != ContainmentType.Disjoint)
                    {
                        modelBatch.DrawSurface(patch, terrainEffect);
                    }
                }
            }
            modelBatch.End();
            

             //-------------
            Matrix world = Matrix.Identity;
            float p=0;
             //-------maze drawing-------

            int rx, ry = 0;
            rx = _creator.returnBegin()[0];
            ry = _creator.returnBegin()[1];





            for (int j = 0; j < _creator.Height; j++)
            {

                for (int i = 0; i < _creator.Width; i++)
                {
                   
                 
                    if (maze[j, i] != 0)
                    {
                        float positionWorld = new float();
                        if (terrain.TryGetHeight(wallHeight*i+2*wallHeight, wallHeight*j+2*wallHeight, out positionWorld))
                        {
                            p = positionWorld;
                        }
                        world = Matrix.CreateTranslation(wallHeight*i+2*wallHeight, wallHeight*j+2*wallHeight, p+wallHeight/2.5f);
                        modelBatch.Begin(viewMatrix,projectionMatrix);
                        modelBatch.DrawPrimitive(mazeCube[i, j], world, kockaEfekt);
                     

                        modelBatch.End();
                       
                    }
                }
            }

            float position = new float();
            if (terrain.TryGetHeight(wallHeight  + 2*wallHeight, wallHeight + 2*wallHeight, out position))
            {
                p = position;
            }
            modelBatch.Begin(viewMatrix, projectionMatrix);
            world = Matrix.CreateTranslation(2*wallHeight + wallHeight * _creator.returnBegin()[0], 2*wallHeight + wallHeight * _creator.returnBegin()[1], 2f + p);

            modelBatch.DrawPrimitive(new Cube(GraphicsDevice, wallHeight), world, kockaEfektPath);

            modelBatch.End();

             

               int movx=0, movy = 0;
               int checkX = 1;
               int checkY = 1;
               bool ind = true;

               for (int i = 0; i < kretanje.Count; i++)
               {
                   if (checkX == mazeSize - 2 && checkY == mazeSize - 2)
                   {
                       statusSolution = 1;
                       resenje = "Nadjeno";
                       break;
                   }
                   switch (kretanje[i])
                   {
                          
        
                       case 3:
                           if (maze[checkY,checkX+1] == 1)
                           {
                               ind = false;
                               break;
                           }
                               movx++;
                               checkX++;

                           if (terrain.TryGetHeight(wallHeight * movx + 2*wallHeight, wallHeight * movy + 2*wallHeight, out position))
                           {
                               p = position;
                           }
                            modelBatch.Begin(viewMatrix, projectionMatrix);
                           world = Matrix.CreateTranslation(wallHeight*movx+ 2*wallHeight + wallHeight*_creator.returnBegin()[0], wallHeight*movy+ 2*wallHeight+ wallHeight*_creator.returnBegin()[1], 2f+p);

                           modelBatch.DrawPrimitive(new Cube(GraphicsDevice, wallHeight), world, kockaEfektPath);

                           modelBatch.End();
                           break;

                       case 2:
                            if (maze[checkY-1,checkX] == 1)
                           {
                               ind = false;
                               break;
                           }
                               
                               checkY--;
                               movy--;
                           if (terrain.TryGetHeight(wallHeight * movx + 2*wallHeight, wallHeight * movy + 2*wallHeight, out position))
                           {
                               p = position;
                           }
                           modelBatch.Begin(viewMatrix, projectionMatrix);
                           world = Matrix.CreateTranslation(wallHeight * movx+2*wallHeight + wallHeight * _creator.returnBegin()[0], movy * wallHeight + 2*wallHeight + wallHeight * _creator.returnBegin()[1], 2f+p);

                           modelBatch.DrawPrimitive(new Cube(GraphicsDevice, wallHeight), world, kockaEfektPath);

                           modelBatch.End();
                           break;

                       case 4:
                           if (maze[checkY+1,checkX] == 1)
                           {
                               ind = false;
                               break;
                           }
                               movy++;
                               checkY++;
                           if (terrain.TryGetHeight(wallHeight * movx + 2*wallHeight, wallHeight * movy + 2*wallHeight, out position))
                           {
                               p = position;
                           }
                           modelBatch.Begin(viewMatrix, projectionMatrix);
                           world = Matrix.CreateTranslation(wallHeight * movx + 2*wallHeight + wallHeight * _creator.returnBegin()[0], movy * wallHeight + 2*wallHeight + wallHeight * _creator.returnBegin()[1], 2f+p);

                           modelBatch.DrawPrimitive(new Cube(GraphicsDevice, wallHeight), world, kockaEfektPath);

                           modelBatch.End();
                           break;

                       case 1:
                            if (maze[checkY,checkX-1] == 1)
                           {
                               ind = false;
                               break;
                           }
                              
                               checkX--;
                           movx--;
                           if (terrain.TryGetHeight(wallHeight * movx + 2*wallHeight, wallHeight * movy + 2*wallHeight, out position))
                           {
                               p = position;
                           }
                           modelBatch.Begin(viewMatrix, projectionMatrix);
                           world = Matrix.CreateTranslation(wallHeight * movx + 2*wallHeight + wallHeight * _creator.returnBegin()[0], movy * wallHeight + 2*wallHeight + wallHeight * _creator.returnBegin()[1], 2f+p);

                           modelBatch.DrawPrimitive(new Cube(GraphicsDevice, wallHeight), world, kockaEfektPath);

                           modelBatch.End();
                           break;

                     
                   }

                   if (ind == false)
                       break;
               }




             //----------------
         //------crtanje menija----------------
         spriteBatch.Begin();
             spriteBatch.Draw(rect, new Rectangle(BackBufferWidth- BackBufferWidth/5, 0, BackBufferWidth/5, BackBufferHeight), Color.White);
             spriteBatch.DrawString(labele, mazeSize.ToString(), new Vector2(BackBufferWidth - 40, 28), Color.Black);
             spriteBatch.DrawString(labele, wallHeight.ToString(), new Vector2(BackBufferWidth - 45, 58), Color.Black);
             spriteBatch.DrawString(labele, moveSpeed.ToString(), new Vector2(BackBufferWidth - 44, 155), Color.Black);
             spriteBatch.DrawString(labele, rotationSpeed.ToString(), new Vector2(BackBufferWidth - 50, 175), Color.Black);
             spriteBatch.DrawString(labele, MutaionScale.ToString(), new Vector2(BackBufferWidth - 55, 340), Color.Black);
             spriteBatch.DrawString(labele,TipMutacije[INDEKSM%TipMutacije.Length], new Vector2(BackBufferWidth - 130, 370), Color.Black);
             spriteBatch.DrawString(labele, TipUkrstanja[INDEKSU % TipUkrstanja.Length], new Vector2(BackBufferWidth - 130, 400), Color.Black);
             spriteBatch.DrawString(labele, TipSkaliranja[INDEKSSK % TipSkaliranja.Length], new Vector2(BackBufferWidth - 130, 430), Color.Black);
             spriteBatch.DrawString(labele, TipSelekcije[INDEKSS % TipSelekcije.Length], new Vector2(BackBufferWidth - 130, 460), Color.Black);
             spriteBatch.DrawString(labele, ""+PopulationSize, new Vector2(BackBufferWidth - 50, 250), Color.Black);
             spriteBatch.DrawString(labele, "" + ChromozomeLength, new Vector2(BackBufferWidth - 50, 280), Color.Black);
             spriteBatch.DrawString(labele, "" + CrossScale, new Vector2(BackBufferWidth - 50, 310), Color.Black);

             spriteBatch.DrawString(labele, "Info: ", new Vector2(BackBufferWidth - 300, 590), Color.Black);
             spriteBatch.DrawString(labele, resenje, new Vector2(BackBufferWidth - 300, 620), Color.Black);
            // spriteBatch.DrawString(labele, "" + CrossScale, new Vector2(BackBufferWidth - 50, 310), Color.Black);




         spriteBatch.End();
             
         //------------------------------------     
             //----crtanje komponenti na meniju----
         gui.Draw();
         

            base.Draw(gameTime);
        }
        //------------ pomeranje kamere----------

        private void ProcessInput(float amount)
        {
            KeyboardState keyState = Keyboard.GetState();
            MouseState currentMouseState = Mouse.GetState();
            if (keyState.IsKeyDown(Keys.LeftControl))
            {
                if (currentMouseState != originalMouseState)
                {
                    float xDifference = currentMouseState.X - originalMouseState.X;
                    float yDifference = currentMouseState.Y - originalMouseState.Y;
                    leftrightRot -= rotationSpeed * xDifference * amount;
                    updownRot -= rotationSpeed * yDifference * amount;
                    Mouse.SetPosition(device.Viewport.Width / 2, device.Viewport.Height / 2);
                    UpdateViewMatrix();
                }
            }

            Vector3 moveVector = new Vector3(0, 0, 0);

            if (keyState.IsKeyDown(Keys.W) || keyState.IsKeyDown(Keys.Up))
                moveVector += new Vector3(0, 0, -1);
            if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
                moveVector += new Vector3(0, 0, 1);
            if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
                moveVector += new Vector3(1, 0, 0);
            if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
                moveVector += new Vector3(-1, 0, 0);
            if (keyState.IsKeyDown(Keys.Q))
                moveVector += new Vector3(0, 1, 0);
            if (keyState.IsKeyDown(Keys.Z))
                moveVector += new Vector3(0, -1, 0);
            AddToCameraPosition(moveVector * amount);
        }

        private void AddToCameraPosition(Vector3 vectorToAdd)
        {
            Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
            Vector3 rotatedVector = Vector3.Transform(vectorToAdd, cameraRotation);
            cameraPosition += moveSpeed * rotatedVector;
            UpdateViewMatrix();
        }

        private void UpdateViewMatrix()
        {
            Matrix cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);

            Vector3 cameraOriginalTarget = new Vector3(0, 0, -1);
            Vector3 cameraOriginalUpVector = new Vector3(0, 1, 0);

            Vector3 cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
            Vector3 cameraFinalTarget = cameraPosition + cameraRotatedTarget;

            Vector3 cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);

            viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraFinalTarget, cameraRotatedUpVector);
        }

        //-------------------------------------------

        public void createMaze(int mazeSize)
        {


            _creator = new MazeCreator(mazeSize, mazeSize, new System.Drawing.Point(1, 1));
            maze = _creator.CreateMaze();
           
        }
        public void callEA()
        {
            ArrayOfInt[] maze1 = new ArrayOfInt[mazeSize];
            ArrayOfInt[] maze2 = new ArrayOfInt[mazeSize];
            //ArrayOfInt niz = new ArrayOfInt();

            for (int i = 0; i < mazeSize; i++)
            {
                maze1[i] = new ArrayOfInt();
                for (int j = 0; j < mazeSize; j++)
                    maze1[i].Add(maze[i, j]);
            }
            for (int i = 1; i < mazeSize - 1; i++)
            {
                maze2[i - 1] = new ArrayOfInt();
                for (int j = 1; j < mazeSize - 1; j++)
                    maze2[i - 1].Add(maze1[i][j]);
            }

            Console.WriteLine("asdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");


        ArrayOfInt rez =  c.callEA(maze2,
                    mazeSize - 2,
                    mazeSize - 2,
                   mazeSize - 2,
                    0,
                    0,
                    3 * mazeSize,
                    30,
                    "Random",
                    "Tournament",
                    "TwoPoints",
                    "Rank",
                     0.6,
                     1
                     );

        Console.WriteLine("asssssssssssssssssssssssssssssssssssa");




        }
        public void getBestPath()
        {
            kretanje = c.getBestFitted();

        }
    }

}
